<?php

namespace App\Http\Controllers\Api;

use App\Model\QandaGood;
use App\Model\QandaModel;
use App\Model\ShopGoodPicModel as ShopGoodPic;
use App\Model\SourceModel;
use App\Model\ShopGoodModel;
use App\Model\ShopTypeModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;


class SourceController extends Controller
{
	//素材库首页
	public function vedio(){
//		$data = [];
//		//返回数据
//		$data['list'] = SourceModel::getList(2);
//		return ['code'=>1,'msg'=>'返回数据','data'=>$data];

		$data = [];
		if(request()->gid){
			$list = SourceModel::where('gid',request()->gid)->where('type',2)->orderBy('sort','desc')->paginate(10);
		}else{
			if(request()->keywords){
				$goods = ShopGoodModel::where('title',"like","%".request()->keywords."%")->select('id')->get();
				$aa = '';
				foreach($goods as $k=>$v){
					$aa .= $v['id'].',';
				}
				$aa = explode(',',$aa);
				$list = SourceModel::whereIn('gid',$aa)->Orwhere("title","like","%".request()->keywords."%")->where('type',2)->orderBy('sort','desc')->paginate(10);
			}elseif(empty(request()->tid) && empty(request()->bid) && empty(request()->label)){
				$list = SourceModel::where('type',2)->paginate(10);
			}else{
				//商品
				$list = ShopGoodModel::where(function ($query) {
					if (request()->tid) {
						//查询子类
						$son_type = ShopTypeModel::where('pid',request()->tid)->get();
						$type =request()->tid.",";
						foreach($son_type as $k=>$v){
							$type .= $v['tid'].",";
						}
						$type = explode(',',trim($type,','));
						$ids = DB::table('shop_good_type')->distinct(true)->whereIn('tid',$type)->pluck('gid');
						$query->whereIn('id', $ids);
//					$query->whereIn('tid', $type);
					}
					if (request()->bid) {
						$query->where('bid', request()->bid);
					}
					if (request()->label) {
						$query->where('labels',"like","%,".request()->label.",%");
					}
				})->select('id')->get();
				$aa = '';
				foreach($list as $k=>$v){
					$aa .= $v['id'].",";
				}
				$ids = explode(',',trim($aa,','));

				$list = SourceModel::whereIn('gid',$ids)->where('type',2)->orderBy('sort','desc')->paginate(10);
			}

		}
		foreach($list as $k=>$v){
			$data[$k]['id'] = $v['id'];
			$data[$k]['title'] = $v['title'];
			$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picname'];
			if($v['oss_pic']){
				$data[$k]['picname'] = $v['oss_pic'];
			}
			$data[$k]['vedio'] = "http://".$_SERVER['HTTP_HOST'].$v['vedio'];
			$data[$k]['click'] = $v['click'];
		}

		$datas['list'] = $data;
		return ['code'=>1,'data'=>$datas];
	}

	//素材库首页
	public function pic(){
		$data = [];
		if(request()->gid){
			$list = SourceModel::where('gid',request()->gid)->where('type',1)->orderBy('sort','desc')->paginate(30);
		}else{
			if(request()->keywords){

				$goods = ShopGoodModel::where('title',"like","%".request()->keywords."%")->select('id')->get();
				$aa = '';
				foreach($goods as $k=>$v){
					$aa .= $v['id'].',';
				}
				$aa = explode(',',$aa);

				$list = SourceModel::whereIn('gid',$aa)->Orwhere('title',"like","%".request()->keywords."%")->orderBy('sort','desc')->where('type',1)->paginate(30);
			}elseif(empty(request()->tid) && empty(request()->bid) && empty(request()->label)){
				$list = SourceModel::where('type',1)->orderBy('sort','desc')->paginate(30);

			}else{
				//商品
				$list = ShopGoodModel::where(function ($query) {
					if (request()->tid) {
						//查询子类
						$son_type = ShopTypeModel::where('pid',request()->tid)->get();
						$type =request()->tid.",";
						foreach($son_type as $k=>$v){
							$type .= $v['tid'].",";
						}
						$type = explode(',',trim($type,','));
//					$query->whereIn('tid', $type);
						$ids = DB::table('shop_good_type')->distinct(true)->whereIn('tid',$type)->pluck('gid');
						$query->whereIn('id', $ids);
					}
					if (request()->bid) {
						$query->where('bid', request()->bid);
					}
					if (request()->label) {
						$query->where('labels',"like","%,".request()->label.",%");
					}
				})->select('id')->get();
				$aa = '';
				foreach($list as $k=>$v){
					$aa .= $v['id'].",";
				}
				$ids = explode(',',trim($aa,','));

				$list = SourceModel::whereIn('gid',$ids)->where('type',1)->orderBy('sort','desc')->paginate(30);
			}

		}
		foreach($list as $k=>$v){
			$data[$k]['id'] = $v['id'];
			$goods = ShopGoodModel::where('id',$v['gid'])->first();
			$data[$k]['title'] = $goods['name'];

			$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$goods['picpath'].$goods['picname'];
			if($goods['oss_pic']){
				$data[$k]['picname'] = $goods['oss_pic'];
			}
		}
		$datas['list'] = $data;
		return ['code'=>1,'data'=>$datas];
	}


	//问答
	public function qanda(){
		if(request()->gid){
			$list = QandaGood::where('gid',request()->gid)->paginate(10);
		}else{
			if(request()->keywords){
				$list = QandaModel::where('question',"like","%".request()->keywords."%")->paginate(10);
				foreach($list as $k=>$v){
					$list[$k]['qid'] = $v['id'];
				}
			}elseif(empty(request()->tid) && empty(request()->bid) && empty(request()->bid) && empty(request()->label)){
				$list = QandaModel::paginate(10);
				foreach($list as $k=>$v){
					$list[$k]['qid'] = $v['id'];
				}
			}else{
				$list = ShopGoodModel::where(function ($query) {
					if (request()->tid) {
						//查询子类
						$son_type = ShopTypeModel::where('pid',request()->tid)->get();
						$type =request()->tid.",";
						foreach($son_type as $k=>$v){
							$type .= $v['tid'].",";
						}
						$type = explode(',',trim($type,','));
//
						$ids = DB::table('shop_good_type')->distinct(true)->whereIn('tid',$type)->pluck('gid');
						$query->whereIn('id', $ids);
					}
					if (request()->bid) {
						$query->where('bid', request()->bid);
					}
					if (request()->label) {
						$query->where('labels',"like","%,".request()->label.",%");
					}
				})->select('id')->get();
				$aa = '';
				foreach($list as $k=>$v){
					$aa .= $v['id'].",";
				}
				$ids = explode(',',trim($aa,','));
				$list = QandaGood::whereIn('gid',$ids)->distinct('qid')->paginate(10);
			}

		}

		$lists = [];
		foreach($list as $k => $v){
			//问答信息
			$qanda = QandaModel::where('id',$v['qid'])->first();

			$lists[$k]['id'] = $qanda['id'];
			$lists[$k]['question'] = $qanda['question'];
			$lists[$k]['answer'] = str_replace('alt','style',$qanda['answer']);
			$lists[$k]['answer'] = str_replace("\"\"",'"display:none"',$lists[$k]['answer'] );
			$lists[$k]['answer'] = str_replace("img",'imga',$lists[$k]['answer'] );
		}
		$datas['list'] = $lists;
		return ['code'=>1,'data'=>$datas];
	}

	//图文详情
	public function pic_detail(){
		if(!request()->tid){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		$data = [];
		$list = SourceModel::where('id',request()->tid)->first();
		$goods = ShopGoodModel::where('id',$list['gid'])->first();
		$data['id'] = $list['id'];
		$data['title'] = $goods['title'];
		$data['content'] = $list['content'];

		//商品图,轮播图
		$picname = "http://".$_SERVER['HTTP_HOST'].$goods['picpath'].$goods['picname'];
		if($goods['oss_pic']){
			$picname = $goods['oss_pic'];
		}
		$pic_cy_c[]['picname'] = $picname;

		$pics = ShopGoodPic::where('gid',$list['gid'])->select('id','picname','oss_pic')->orderBy('id','desc')->limit(9)->get();
		if(count($pics) >= 0){
			foreach($pics as $k=>$v){
				$picname_cy = 'http://'.$_SERVER['HTTP_HOST'].$v['picname'];
				if($v['oss_pic']){
					$picname_cy = $v['oss_pic'];
				}
				$pic_cy_c[]['picname'] = $picname_cy;
			}
		}else{
			$pics = [];
		}

		$data['picname'] = $pic_cy_c;

		return ['code'=>1,'msg'=>'返回数据','data'=>$data];
	}

	//增加下载量
	public function click(){
		if(!request()->tid){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		DB::beginTransaction();
		try{
			SourceModel::where('id',request()->tid)->increment('click');
			DB::commit();
			return ['code'=>1,'msg'=>'增加下载量'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'下载量增加失败'];
		}
	}

	//筛选素材库
	public function search(){
		$data = [];
		//商品
		$list = ShopGoodModel::where(function ($query) {
			if (request()->tid) {
				//查询子类
				$son_type = ShopTypeModel::where('pid',request()->tid)->get();
				$type =request()->tid.",";
				foreach($son_type as $k=>$v){
					$type .= $v['tid'].",";
				}
				$type = explode(',',trim($type,','));
//				$query->whereIn('tid', $type);
				$ids = DB::table('shop_good_type')->distinct(true)->whereIn('tid',$type)->pluck('gid');
				$query->whereIn('id', $ids);
			}
			if (request()->bid) {
				$query->where('bid', request()->bid);
			}
			if (request()->label) {
				$query->where('labels',"like","%,".request()->label.",%");
			}
		})->select('id')->get();
		$aa = '';
		foreach($list as $k=>$v){
			$aa .= $v['id'].",";
		}
		$ids = explode(',',trim($aa,','));

		$list = SourceModel::whereIn('gid',$ids)->get();
		foreach($list as $k=>$v){
			$data[$k]['id'] = $v['id'];
			$data[$k]['title'] = $v['title'];
			$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picname'];
			if($v['oss_pic']){
				$data[$k]['picname'] = $v['oss_pic'];
			}
		}
		$datas['list'] = $data;
		return ['code'=>1,'data'=>$datas];
	}

}